package com.yunfeng.supercall.manager;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalDb;
import android.content.Context;
import android.preference.PreferenceManager;

import com.yunfeng.supercall.BuildConfig;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.SuperFriendsDelegateBean;

public class SuperFriendsManager extends ManagerBaseIface<SuperFriendsBean> {

	private FinalDb mFinalDb;

	public SuperFriendsManager init(Context context) {
		mFinalDb = FinalDb.create(context, "superfriends", BuildConfig.DEBUG);
		mList = new ArrayList<SuperFriendsBean>();
		return this;
	}

	@Override
	public void saveList(final List<SuperFriendsBean> list) {
		// TODO Auto-generated method stub
		mList.clear();
		// mFinalDb.deleteAll(SuperFriendsDelegate.class);
		mList.addAll(list);
		new Thread() {
			public void run() {
				mFinalDb.deleteAll(SuperFriendsDelegateBean.class);
				for (SuperFriendsBean s : list) {
					SuperFriendsDelegateBean delegate = new SuperFriendsDelegateBean(s);
					try {
						mFinalDb.save(delegate);
					} catch(Exception e) {

					}
				}
			};
		}.start();

	}

	/**
	 * 返回匹配的好友
	 * 
	 * @param phone
	 * @return
	 */
	public SuperFriendsBean findSuperFriendsByPhone(String phone) {
		List<SuperFriendsBean> mFriends = (List<SuperFriendsBean>) mList;
		if (mFriends == null) {
			return null;
		}
		for (SuperFriendsBean f : mFriends) {
			if (f.getPhone().equals(phone) && !f.isContactsFromPhone()) {
				return f;
			}
		}
		return null;
	}

	public void updateFriends(SuperFriendsBean friends) {
		SuperFriendsDelegateBean delegate = new SuperFriendsDelegateBean(friends);
		mFinalDb.update(delegate);
	}

	public SuperFriendsBean findSuperFriendsByYZXNumber(String number) {
		List<SuperFriendsBean> mFriends = (List<SuperFriendsBean>) mList;
		if (mFriends == null) {
			return null;
		}
		for (SuperFriendsBean f : mFriends) {
			if (!f.isContactsFromPhone() && number.equals(f.getVo_id())) {
				return f;
			}
		}
		return null;
	}

	// @Override
	// public List<SuperFriends> getList() {
	// // TODO Auto-generated method stub
	// mList.clear();
	// mList.addAll(mFinalDb.findAll(SuperFriends.class));
	// return mList;
	// }

	@Override
	public List<SuperFriendsBean> getList() {
		// TODO Auto-generated method stub
		mList.clear();
		List<SuperFriendsDelegateBean> delegates = mFinalDb
				.findAll(SuperFriendsDelegateBean.class);
		for (SuperFriendsDelegateBean delegate : delegates) {
			SuperFriendsBean s = new SuperFriendsBean(delegate);
			mList.add(s);
		}
		return mList;
	}

	public List<SuperFriendsBean> getListInCache() {
		if (mList == null) {
			mList = new ArrayList<SuperFriendsBean>();
		}
		return mList;
	}

	public void reloadFriends() {
		if (mList == null) {
			// mList =new ArrayList<E>();
		}
	}

	public static boolean hasNewFriend(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean("has", false);
	}

	public static void setHasNewFriend(Context context, boolean has) {
		PreferenceManager.getDefaultSharedPreferences(context).edit()
		.putBoolean("has", has).commit();
	}
}
